package com.wmcsk.dl.util;

import android.support.annotation.NonNull;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

import com.wmcsk.dl.anum.CheckMust;
import com.wmcsk.dl.bean.ResponseMsgBean;
import com.wmcsk.dl.listener.Checkable;


/**
 * Created by wm on 2016/12/6.
 */
public class CheckUtils {
    public static ResponseMsgBean check(@NonNull Checkable checkable){
       return checkable==null?new ResponseMsgBean(-1,"checkable is null"):checkMust(checkable);
    }
    public static ResponseMsgBean checkMust(@NonNull Checkable checkable){
        try {
            Field[] fields = checkable.getClass().getDeclaredFields();
            if(fields == null){
                return new ResponseMsgBean(0,"ok");
            }
            for(Field field:fields){
                field.setAccessible(true);
                boolean must = getMustValue(field);
                if(must){
                    boolean isNull = isNull(checkable,field);
                    if(isNull){
                        return new ResponseMsgBean(-1,field.getName()+"must not be null");
                    }
                }
            }
        }catch (Exception e){

        }
        return new ResponseMsgBean(0,"ok");
    }
    /**
     * 获取需要转换的值
     * @param field
     * @return
     */
    private static boolean getMustValue(Field field){
        Annotation[] annotations = field.getAnnotations();
        if(annotations == null||annotations.length == 0){
            return false;
        }
        CheckMust checkMust = null;
        for(int i = 0;i<annotations.length;i++){
            Annotation a = annotations[i];
            if(a instanceof CheckMust){
                checkMust = ((CheckMust) a);
                break;
            }
        }
        if(checkMust == null){
            return false;
        }
        boolean value = checkMust.value();
        return value;
    }

    private static boolean isNull(Object object, Field field){
        try {
           Object o = field.get(object);
           return o == null;
        }catch (Exception e){

        }
        return false;
    }

    /**
     * 比较版本号
     * @param version1
     * @param version2
     * @return
     */
    public static int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");

        int minLen = Math.min(version1Array.length, version2Array.length);
        int diffLength = version1Array.length - version2Array.length;
        int diff = 0;

        for(int i = 0;i<minLen;i++){
            diff = Integer.parseInt(version1Array[i]) - Integer.parseInt(version2Array[i]);
            if(diff!=0){
                return diff;
            }
        }
        if(diffLength == 0){
            return 0;
        }else if(diffLength>0){
            for(int i = minLen;i<version1Array.length;i++){
                int current = Integer.parseInt(version1Array[i]);
                if(current != 0){
                    return current;
                }
            }
        }else{
            for(int i = minLen;i<version2Array.length;i++){
                int current = Integer.parseInt(version2Array[i]);
                if(current != 0){
                    return -current;
                }
            }
        }
        return 0;
    }
}
